package com.example.sunmiv1.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.StorePurchaseOrderInfo;
import com.example.sunmiv1.utils.net.ACache;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PurchaseOrderAdapter extends BaseAdapter {

    private Context context;
    private List<StorePurchaseOrderInfo.DataBean> list;
    private ViewHolder viewHolder;
    private Operation operation;
    private String pricePermission;// 門店查看價格權限

    public PurchaseOrderAdapter(Context context, List<StorePurchaseOrderInfo.DataBean> list) {
        this.context = context;
        this.list = list;
        ACache aCache = ACache.get(context);
        pricePermission = aCache.getAsString("showPrice");
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.purchase_order_adapter, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        StorePurchaseOrderInfo.DataBean dataBean = list.get(position);
        viewHolder.purchaseOrderPeople.setText(dataBean.getOperator());
        viewHolder.purchaseOrderPurchaseTime.setText(dataBean.getAdd_time());
        viewHolder.purchaseOrderOperationTime.setText(dataBean.getRepair_time());
        viewHolder.purchaseOrderPurchaseOrderNo.setText(dataBean.getOrder_number());
        viewHolder.purchaseOrderType.setText(dataBean.getType_name());
        viewHolder.spinnerSupplies.setText(dataBean.getSupplier());
        viewHolder.spinnerWarehouseChoose.setText(dataBean.getWarehouse());
        viewHolder.purchaseOrderTotal.setText(dataBean.getGoods_species());
        viewHolder.purchaseOrderMoney.setText(dataBean.getMoney_total());
        if (!dataBean.getPay_status().equals("2") && !dataBean.getPay_status().equals("3")) {
            viewHolder.purchaseOrderUpdate.setVisibility(View.VISIBLE);
            viewHolder.purchaseOrderUpdate.setText(R.string.update);
        } else {
            viewHolder.purchaseOrderUpdate.setVisibility(View.INVISIBLE);
        }
        if (dataBean.getWarehouse_state() == 0) {
            viewHolder.purchaseOrderWarehousing.setVisibility(View.VISIBLE);
        } else {
            viewHolder.purchaseOrderWarehousing.setVisibility(View.INVISIBLE);
        }
        if (TextUtils.isEmpty(dataBean.getRemarks())) {
            viewHolder.groupRemark.setVisibility(View.GONE);
        } else {
            viewHolder.groupRemark.setVisibility(View.VISIBLE);
            viewHolder.infoRemark.setText(dataBean.getRemarks());
        }
        final int index = position;
        /**
         * 查看詳情
         * */
        viewHolder.purchaseOrderDetail.setOnClickListener(v -> {
            operation.success(3, index);
        });
        /**
         * 編輯
         * */
        viewHolder.purchaseOrderUpdate.setOnClickListener(v -> {
            operation.success(1, index);
        });

        /**
         * 入庫
         * */
        viewHolder.purchaseOrderWarehousing.setOnClickListener(v -> {
            operation.success(2, index);
        });
        if ("2".equals(pricePermission)) {
            viewHolder.money.setVisibility(View.GONE);
        }
        return convertView;
    }

    public interface Operation {
        void success(int type, int index);
    }

    public void Click(Operation operation) {
        this.operation = operation;
    }


    static class ViewHolder {
        @BindView(R.id.purchaseOrder_people)
        TextView purchaseOrderPeople;
        @BindView(R.id.purchaseOrder_purchaseTime)
        TextView purchaseOrderPurchaseTime;
        @BindView(R.id.purchaseOrder_operationTime)
        TextView purchaseOrderOperationTime;
        @BindView(R.id.purchaseOrder_purchaseOrderNo)
        TextView purchaseOrderPurchaseOrderNo;
        @BindView(R.id.purchaseOrder_type)
        TextView purchaseOrderType;
        @BindView(R.id.spinner_supplies)
        TextView spinnerSupplies;
        @BindView(R.id.spinner_warehouse_choose)
        TextView spinnerWarehouseChoose;
        @BindView(R.id.purchaseOrder_total)
        TextView purchaseOrderTotal;
        @BindView(R.id.purchaseOrder_money)
        TextView purchaseOrderMoney;
        @BindView(R.id.money)
        LinearLayout money;
        @BindView(R.id.info_remark)
        TextView infoRemark;
        @BindView(R.id.group_remark)
        LinearLayout groupRemark;
        @BindView(R.id.purchaseOrder_detail)
        TextView purchaseOrderDetail;
        @BindView(R.id.purchaseOrder_update)
        TextView purchaseOrderUpdate;
        @BindView(R.id.purchaseOrder_warehousing)
        TextView purchaseOrderWarehousing;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
